using System;
using System.Data;
using System.Data.SqlClient;

#if SQL2005
namespace SBPweb.Data.SQL2005
{

	/// <summary>
	/// This class describes a parameter for stored procedure
	/// </summary>
	public class SqlParameterDescriptor
	{

		#region Private fields

		private Type mFieldType;
		private SqlParameter mParameter;
        private object mValue;

		#endregion

		#region Constructor logic

		/// <summary>
		/// Initializes a new SqlParameterDescriptor instance
		/// </summary>
		private SqlParameterDescriptor()
		{
		}

		/// <summary>
		/// Initializes a new SqlParameterDescriptor instance
		/// </summary>
		/// <param name="entityFieldName">Field name in entity and db</param>
		/// <param name="fieldType">Type of field</param>
		public SqlParameterDescriptor(string entityFieldName, Type fieldType)
			: this(entityFieldName, entityFieldName, fieldType)
		{
		}

		/// <summary>
		/// Initializes a new SqlParameterDescriptor instance
		/// </summary>
		/// <param name="entityFieldName">Field name in entity</param>
		/// <param name="parameterName">Field name in db</param>
		/// <param name="fieldType">Type of field</param>
		public SqlParameterDescriptor(string entityFieldName, string parameterName, Type fieldType)
			: this(entityFieldName, parameterName, fieldType, SqlDatabase.GetDbTypeByType(fieldType))
		{
		}

		/// <summary>
		/// Initializes a new SqlParameterDescriptor instance
		/// </summary>
		/// <param name="entityFieldName">Field name in entity and db</param>
		/// <param name="fieldType">Type of field in entity</param>
		/// <param name="paramDbType">Type of field in db</param>
		public SqlParameterDescriptor(string entityFieldName, Type fieldType, SqlDbType paramDbType)
			: this(entityFieldName, entityFieldName, fieldType, paramDbType)
		{
		}

		/// <summary>
		/// Initializes a new SqlParameterDescriptor instance
		/// </summary>
		/// <param name="entityFieldName">Field name in entity and db</param>
		/// <param name="fieldType">Type of field in entity</param>
		/// <param name="paramDbType">Type of field in db</param>
		/// <param name="direction">Direction of the parameter</param>
		public SqlParameterDescriptor(string entityFieldName, Type fieldType, SqlDbType paramDbType, ParameterDirection direction)
			: this(entityFieldName, entityFieldName, fieldType, paramDbType, direction, null)
		{
		}

		/// <summary>
		/// Initializes a new SqlParameterDescriptor instance for input
		/// </summary>
		/// <param name="entityFieldName">Field name in entity</param>
		/// <param name="parameterName">Field name in db</param>
		/// <param name="fieldType">Type of field in entity</param>
		/// <param name="paramDbType">Type of field in db</param>
		public SqlParameterDescriptor(string entityFieldName, string parameterName, Type fieldType, SqlDbType paramDbType)
			: this(entityFieldName, parameterName, fieldType, paramDbType, ParameterDirection.Input, null)
		{
		}

		/// <summary>
		/// Initializes a new SqlParameterDescriptor instance for input
		/// </summary>
		/// <param name="parameterName">Field name in db</param>
		/// <param name="paramDbType">Type of field in db</param>
		/// <param name="value">Value of the field</param>
        public SqlParameterDescriptor(string parameterName, SqlDbType paramDbType, object value)
            : this(parameterName, parameterName, typeof(object), paramDbType, ParameterDirection.Input, value)
        {
        }

		/// <summary>
		/// Initializes a new SqlParameterDescriptor instance 
		/// </summary>
		/// <param name="entityFieldName">Field name in entity</param>
		/// <param name="parameterName">Field name in db</param>
		/// <param name="fieldType">Type of field in entity</param>
		/// <param name="paramDbType">Type of field in db</param>
		/// <param name="direction">Direction of the parameter</param>
		/// <param name="value">Value of the parameter</param>
		public SqlParameterDescriptor(string entityFieldName, string parameterName, Type fieldType, SqlDbType paramDbType, ParameterDirection direction, object value)
		{
            mValue = value;
			mParameter = new SqlParameter(parameterName, null);
			mParameter.SourceColumn = entityFieldName;
			mParameter.SqlDbType = paramDbType;
			mParameter.Direction = direction;
			mFieldType = fieldType;
		}

		/// <summary>
		/// Initializes a new SqlParameterDescriptor instance
		/// </summary>
		/// <param name="parameter">SqlParameter parameter</param>
		internal SqlParameterDescriptor(SqlParameter parameter)
		{
			mParameter = parameter;
		}

		#endregion

		#region Public property accessors

		/// <summary>
		/// Returns the name of the field in entity
		/// </summary>
		public string FieldName
		{
			get
			{
				return mParameter.SourceColumn;
			}
		}

		/// <summary>
		/// Returns the name of the field in db
		/// </summary>
		public string ParameterName
		{
			get
			{
				return mParameter.ParameterName;
			}
		}

		/// <summary>
		/// Returns the Type of the field in entity
		/// </summary>
		public Type FieldType
		{
			get
			{
				return mFieldType;
			}
		}

		/// <summary>
		/// Returns the type of the field in db
		/// </summary>
		public SqlDbType ParameterType
		{
			get
			{
				return mParameter.SqlDbType;
			}
		}

		/// <summary>
		/// Returns the direction of the parameter
		/// </summary>
		public ParameterDirection Direction
		{
			get
			{
				return mParameter.Direction;
			}
		}

		/// <summary>
		/// Returns the value of the parameter
		/// </summary>
		public object Value
        {
            get
            {
                return mValue;
            }
        }

		#endregion

		/// <summary>
		/// SqlParameter Parameter
		/// </summary>
		internal SqlParameter Parameter
		{
			get
			{
				return mParameter;
			}
		}
	}
}
#endif